from robot_controller import RobotController

class RobotsManager(object):
    def __init__(self, world):
        robots = world.getRobots()
        comm_channel = world.getCommunicationChannel()
        timer = world.getTimer()
        map = world.getMap()
        self._robot_controllers = [RobotController(i, comm_channel, timer, map) for i in range(len(robots))]
    
    def getRobotControllers(self):
        return self._robot_controllers
        
    def getRobotControllersNum(self):
        return len(self._robot_controllers)
        
    def getRobotController(self, idx):
        return self._robot_controllers[idx]

    def process(self, dt):
        #processed_something = True
        #while processed_something:
        for ctrl in self._robot_controllers:
            processed_something = ctrl.process(dt)
                
    def getPosUpdatedRobot(self):
        for c in self._robot_controllers:
            if c.isPosUpdated():
                c.clearPosUpdatedFlag()
                return c
        return None
        
    def clearAllRobotPosUpdatedFlag(self):
        for c in self._robot_controllers:
            c.clearPosUpdatedFlag()
            
    def getNewTaskArrivedRobot(self):
        controllers = []
        for c in self._robot_controllers:
            if c.isNewTaskArrived():
                controllers.append(c)
        if controllers:
            return controllers
        else:
            return None
        
    def clearAllRobotNewTaskArrivedFlag(self):
        for c in self._robot_controllers:
            c.clearNewTaskArrivedFlag()
        